const res = require('express/lib/response')
var dbConfig = require('../utils/dbConfig')
let fs = require('fs')
getSchoolList = (req, res) => {
  let sql = 'select * from school'
  let sqlArr = []
  let callBack = (err, data) => {
    if (err) {
      console.log(err)
    } else {
      res.send({
        'code': 200,
        'data': data
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
getClassList = (req, res) => {
  let { schoolID } = req.query
  let sql = 'select * from class where schoolID=?'
  let sqlArr = [schoolID]
  let callBack = (err, data) => {
    if (err) {
      console.log(err)
    } else {
      res.send({
        'code': 200,
        'data': data
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
},

  getCart = async (req, res) => {
    let { userID } = req.query
    let cart = await selectMomentID(userID)
    let cartID = []
    for (let i = 0; i < cart.length; i++) {
      cartID.push(cart[i].momentID)
    }
    if (cartID.length === 0) cartID.push(0)
    let sql = 'select * from moment where momentID in (?)'
    let sqlArr = [cartID]
    let callBack = (err, data) => {
      if (err) {
        console.log(err)
      } else {
        res.send({
          'code': 200,
          'data': data
        })
      }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
  },
  deleteGoods = async (req, res) => {
    let body = []
    for (let key in req.body) {
      body.push(parseInt(req.body[key]))
    }
    let sql = 'delete from user_cart where userID=? and momentID in (?)'
    let sqlArr = [body[0], body.slice(1)]
    const result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result.affectedRows > 0) {
      res.send({
        'code': 200,
        'msg': "删除成功"
      })
    } else {
      res.send({
        'code': 400,
        'msg': "删除失败"
      })
    }
  },
  getUserAddress = (req, res) => {
    let { userID } = req.query
    let sql = 'select * from user_address where userID=?'
    let sqlArr = [userID]
    let callBack = (err, data) => {
      if (err) {
        console.log(err)
      } else {
        res.send({
          'code': 200,
          'data': data
        })
      }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
  }
getAddressByID = (req, res) => {
  let { addressID } = req.query
  let sql = 'select * from user_address where addressID=?'
  let sqlArr = [addressID]
  let callBack = (err, data) => {
    if (err) {
      console.log(err)
    } else {
      res.send({
        'code': 200,
        'data': data
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
lockMoment = async (req, res) => {
  let momentID = []
  for (let key in req.body) {
    momentID.push(parseInt(req.body[key]))
  }
  let sql = 'select momentID,status from moment where momentID in (?)'
  let sqlArr = [momentID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  let newMomentID = []
  for (let i in result) {
    if (result[i].status === 0 || result[i].status === null) {
      newMomentID.push(result[i].momentID)
    }
  }
  if (newMomentID.length !== momentID.length) {
    res.send({
      'code': 400,
      'data': [],
      'msg': "当前物品已被锁定"
    })
    return
  }
  sql = 'update moment set status = 1 where momentID in (?)'
  sqlArr = [newMomentID]
  let callBack = (err, data) => {
    if (err) {
      console.log(err)
    } else {
      res.send({
        'code': 200,
        'data': newMomentID,
        'msg': "获取数据成功"
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
unlockMoment = async (req, res) => {
  let momentID = []
  for (let key in req.body) {
    momentID.push(parseInt(req.body[key]))
  }
  console.log(momentID)
  let sql = 'update moment set status =0 where momentID in (?)'
  let sqlArr = [momentID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows >= 1) {
    res.send({
      'code': 200,
      'msg': "解除锁定成功"
    })
  } else {
    res.send({
      'code': 400,
      'msg': "解除锁定失败"
    })
  }
}
saveOrder = async (req, res) => {
  let { sellerID, addressID, momentID, buyerID } = req.body
  let sql = 'update moment set status =2 where momentID=?'
  let sqlArr = [momentID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    let orderID = (new Date()).valueOf() + (momentID).toString()
    sql = 'insert into user_order(orderID,sellerID,buyerID,momentID,addressID,status,create_time) values (?,?,?,?,?,?,?)'
    sqlArr = [orderID, sellerID, buyerID, momentID, addressID, 0, (new Date()).valueOf()]
    result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result.affectedRows === 1) {
      res.send({
        'code': 200,
        'msg': '购买成功'
      })
    } else {
      res.send({
        'code': 400,
        'msg': '购买失败'
      })
    }
  }

}
getMyOrder = (req, res) => {
  let { userID } = req.query
  let sql = 'select * from user_order where buyerID=? order by create_time desc'
  let sqlArr = [userID]
  let callBack = (err, data) => {
    if (err) {
      console.log(err)
    } else {
      res.send({
        'code': 200,
        'data': data
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
// 确认接单
takeOrder = async (req, res) => {
  let { orderID } = req.body
  let sql = 'update user_order set status=1 where orderID=?'
  let sqlArr = [orderID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '接单成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '接单失败'
    })
  }
}
// 确认签收卖家
takeSignSeller = async (req, res) => {
  let { orderID } = req.body
  let sql = 'select status from user_order where orderID=?'
  let sqlArr = [orderID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result[0].status === 1) {
    sql = 'update user_order set status=2 where orderID=?'
  } else if (result[0].status === 3) {
    sql = 'update user_order set status=4 where orderID=?'
  }
  let results = await dbConfig.SySqlConnect(sql, sqlArr)
  if (results.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '失败'
    })
  }
}
// 确认签收买家
takeSignBuyer = async (req, res) => {
  let { orderID } = req.body
  let sql = 'update user_order set status=4 where orderID=?'
  let sqlArr = [orderID]
  let results = await dbConfig.SySqlConnect(sql, sqlArr)
  if (results.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '失败'
    })
  }
}
// 取消订单
cancelOrder = async (req, res) => {
  let { orderID } = req.body
  let sql = 'update user_order set status=-1 where orderID=?'
  let sqlArr = [orderID]
  let results = await dbConfig.SySqlConnect(sql, sqlArr)
  if (results.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '取消成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '取消失败'
    })
  }
}

getUserCare = async (req, res) => {
  let { userID } = req.query
  let sql = 'select careID from user_care where userID = ? '
  let sqlArr = [userID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  let careID = []
  console.log(result)
  for (let key in result) {
    careID.push(result[key].careID)
  }
  console.log(careID)
  sql = 'select userID,avator,nickname from user where userID in (?)'
  sqlArr = [careID]
  let callBack = (err, data) => {
    if (err) {
      console.log(err)
    } else {
      res.send({
        'code': 200,
        'data': data,
        'msg': "获取数据成功"
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
let selectMomentID = (userID) => {
  let sql = 'select momentID from user_cart where userID=?'
  let sqlArr = [userID]
  return dbConfig.SySqlConnect(sql, sqlArr)
}
certify = async (req, res) => {
  let { school, college, Class, userID } = req.body
  let files = req.files
  console.log(req.body)
  let fileList = ''
  for (let i in files) {
    let file = files[i];
    fs.renameSync('./public/uploads/' + file.filename, './public/uploads/certify/' + file.originalname)
    fileList = `http://localhost:3000/uploads/certify/${file.originalname}`
  }
  let sql = 'update user set school=?,college=?,class=?,certifyImg=? where userID=?'
  let sqlArr = [school, college, Class, fileList, userID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': "请耐心等待审核"
    })
  } else {
    res.send({
      'code': 400,
      'msg': "好像除了一点小问题"
    })
  }
}
module.exports = {
  getSchoolList,
  getClassList,
  certify,
  getCart,
  deleteGoods,
  getUserAddress,
  getAddressByID,
  lockMoment,
  unlockMoment,
  saveOrder,
  takeOrder,
  takeSignSeller,
  takeSignBuyer,
  cancelOrder,
  getMyOrder,
  getUserCare,
}